package com.example.infowall02.mapper;

import com.example.infowall02.domain.Articles;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticlesMapper {

//增
    //添加活动
    @Insert("INSERT INTO articles (type, theme, rule, date, location, sign_method, origin, image, content) " +
            "VALUES (#{type}, #{theme}, #{rule}, #{date}, #{location}, #{signMethod}, #{origin}, #{image}, #{content})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "article_id")
    int addArticle(Articles article);

//删
    //根据id删除文章
    @Delete("DELETE FROM articles WHERE article_id = #{id}")
    int deleteArticleById(int id);

//查
    //根据id查找
    @Select("SELECT article_id as id, type, theme, rule, date, location, sign_method as signMethod, " +
            "origin, likes, clicks, favorites, image, content" +
            " FROM articles WHERE article_id = #{id}")
    Articles findArticleById(int id);
    //根据类别查找，分页查询
    @Select("SELECT article_id AS id, type, theme, rule, date, location, sign_method as signMethod," +
            "origin, likes, clicks, favorites, image, content " +
            "FROM articles WHERE type = #{type} "+
            "LIMIT #{pageNow}, #{pageSize}")
    List<Articles> findArticlesByType(String type,int pageNow,int pageSize);
    // 根据输入内容在类别和主题中进行模糊搜索，分页查询
    @Select("SELECT article_id AS id, type, theme, rule, date, location, sign_method as signMethod, " +
            "origin, likes, clicks, favorites, image, content FROM articles " +
            "WHERE type LIKE CONCAT('%', #{keyword}, '%') OR theme LIKE CONCAT('%', #{keyword}, '%') " +
            "LIMIT #{pageNow}, #{pageSize}")
    List<Articles> findArticlesByKeyword(@Param("keyword") String keyword, @Param("pageNow") int pageNow, @Param("pageSize") int pageSize);
    //根据keyword返回总查询数
    @Select("SELECT COUNT(*) FROM articles WHERE type LIKE CONCAT('%', #{keyword}, '%') OR theme LIKE CONCAT('%', #{keyword}, '%')")
    int countArticlesByKeyword(@Param("keyword") String keyword);
    //根据类别返回总查询数
    @Select("SELECT COUNT(*) FROM articles WHERE type = #{type}")
    int countArticlesByType(String type);
    //order by id 只查五条
    @Select("SELECT article_id AS id, type, theme, rule, date, location, sign_method as signMethod," +
            " origin, likes, clicks, favorites, image, content" +
            " FROM articles ORDER BY article_id DESC LIMIT 5")
    List<Articles> findRecommendArticlesByTime();
    //order by clicks 只查5条
    @Select("SELECT article_id AS id, type, theme, rule, date, location, sign_method as signMethod, " +
            "origin, likes, clicks, favorites, image, content" +
            " FROM articles ORDER BY clicks DESC LIMIT 5")
    List<Articles> findRecommendArticlesByClicks();
    //查找是否有对应id的文章
    @Select("SELECT COUNT(*) FROM articles WHERE article_id = #{articleId}")
    int countArticleById(@Param("articleId") int articleId);

//改
    //增加点赞数
    @Update("UPDATE articles SET likes = likes + 1 WHERE article_id = #{articleId}")
    int incrementLikes(int articleId);
    //减少点赞数
    @Update("UPDATE articles SET likes = likes - 1 WHERE article_id = #{articleId}")
    int decrementLikes(int articleId);
    //增加点击量
    @Update("UPDATE articles SET clicks = clicks + 1 WHERE article_id = #{id}")
    int incrementClicks(int id);
    //增加收藏数
    @Update("UPDATE articles SET favorites = favorites + 1 WHERE article_id = #{id}")
    int incrementFavorites(int id);
    // 更新文章全部信息
    @Update("UPDATE articles SET type = #{article.type}, theme = #{article.theme}, rule = #{article.rule}, date = #{article.date}, " +
            "location = #{article.location}, sign_method = #{article.signMethod}, origin = #{article.origin}, image = #{article.image}, " +
            "content = #{article.content} WHERE article_id = #{id}")
    int updateArticleInfo(@Param("article") Articles article, @Param("id") int id);

    @Update("UPDATE articles SET favorites = favorites - 1 WHERE article_id = #{articleId} AND favorites > 0")
    int decrementFavorites(int articleId);
}
